package com.chinaedu.oc.service.manageuser;

import java.util.Optional;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.chinaedu.oc.context.Common.Session;
import com.chinaedu.oc.entity.manageuser.ManageUser;


@Service
public class ManageUserLoginService {

	/**
	 * 判断管理员用户是否登录
	 *
	 * @param request
	 * @return
	 */
	public Optional<ManageUser> validateManageUserLogin(HttpServletRequest request) {
		ManageUser manageUser = (ManageUser) request.getSession().getAttribute(Session.manageUserLogin.name());

		return Optional.ofNullable(manageUser);
	}

	/**
	 * 设置管理员用户登录
	 *
	 * @param request
	 */
	public void writeManageUserLogin(ManageUser manageUser, HttpServletRequest request) {
		request.getSession().setAttribute(Session.manageUserLogin.name(), manageUser);
	}

	/**
	 * 生成管理员密码
	 *
	 * @param password
	 * @return
	 */
	public  String generatePassword() {
//		for (int i = 0; i < 1024; i++) {
//			enc = DigestUtils.generateMD5(password, Keys.MANAGE_USER_PWD_KEY.key(), enc);
//		}
		StringBuffer str=new StringBuffer();//定义变长字符串
		Random random=new Random();
		//随机生成数字，并添加到字符串
		for(int i=0;i<8;i++){
		    str.append(random.nextInt(10));
		}
		return str.toString();
	}
	

	/**
	 * 管理员退出登录
	 *
	 * @param session
	 */
	public void logout(HttpSession session) {
		session.removeAttribute(Session.manageUserLogin.name());
	}
	
}
